Step 3 - Define and use a static list data source

In this step of the tutorial you first define the data source for a static list in the data source plugin and then use it in the Kanzi Studio project to get the data for items in a Grid List Box node from a data source.

Add the functionality to generate list data objects

In this section you add the functionality which enables the data source plugin to generate list data objects.

To add the functionality to generate list data objects:

  1. In Windows Explorer from the <KanziWorkspace>/Tutorials/Data sources/Assets/Static list directory copy the data_object_static_list.cpp and data_object_static_list.hpp files to the <KanziWorkspace>/Tutorials/Data sources/Start/Application/src/plugin/src directory.
    These files provide the functionality for creating data objects for a static list.
  2. In Visual Studio in the Solution Explorer right-click the XML_data_source project, select Add > Existing Item, in the <KanziWorkspace>/Tutorials/Data sources/Start/Application/src/plugin/src directory select the data_object_static_list.cpp and data_object_static_list.hpp files, and click Add.
  3. In Visual Studio make these changes to the xmldatasource.cpp:
    1. Include the data_object_static_list.hpp header file.
      // Provides the functionality for creating data objects for a static list.
      #include "data_object_static_list.hpp"
    2. After the onTimer function create a function which converts the content of the XML structure in the memory to list items in the static list data object.
      static void addDataObjectsRecursively(Domain* domain, DataObjectSharedPtr object, const tinyxml2::XMLElement* xml);
      
      // Use this function to add list items to a list data object.
      static void addListItems(Domain* domain, DataObjectStaticListSharedPtr list, const tinyxml2::XMLElement* itemsElement)
      
      {
          for (const tinyxml2::XMLElement* child = itemsElement->FirstChildElement(); child; child = child->NextSiblingElement())
          {
              // Create the root data object for the list item hierarchy.
              DataObjectSharedPtr rootObject = make_shared<DataObject>(domain, "Item root");
              // Create the list item tree recursively.
              addDataObjectsRecursively(domain, rootObject, child);
              // Add the first child from the root data object as a list item. Discard the root object.
              list->addItem(rootObject);
          }
      }
    3. Add to the addDataObject function a condition to create a list type data object from the list type attribute.
      // Add a data object of the type specified by the type attribute in the XML element. Get the initial value from the text parameter.
      DataObjectSharedPtr addDataObject(Domain *domain, const char* type, const char* name, const char* text)
      {
      
      ...
      
          // Create a list data object from the list type attributes.
          else if (type && strcmp(type, "list") == 0)
          {
              object = make_shared<DataObjectStaticList>(domain, name);
          }
      
      ...
      
      }
    4. Change the addDataObjectsRecursively function to add the functionality to create data objects inside a list data object, if the name of the XML element within the list type is called items.
      Replace
      static void addDataObjectsRecursively(Domain* domain, DataObjectSharedPtr parent, const tinyxml2::XMLElement* xml)
      {
      
      ...
      
          // Traverse the tree in the XML file to add data objects for each child element of the current XML element.
          for (const tinyxml2::XMLElement* child = xml->FirstChildElement(); child; child = child->NextSiblingElement())
          {
              // Recurse.
              addDataObjectsRecursively(domain, object, child);
          }
      }

      with
      static void addDataObjectsRecursively(Domain* domain, DataObjectSharedPtr parent, const tinyxml2::XMLElement* xml)
      {
      
      ...
          // Check whether the type of the current data object is list.
          DataObjectStaticListSharedPtr list = dynamic_pointer_cast<DataObjectStaticList>(object);
          // Traverse the tree in the XML file to add data objects for each child element of the current XML element.
          for (const tinyxml2::XMLElement* child = xml->FirstChildElement(); child; child = child->NextSiblingElement())
          {
              // If the name of the XML element within the list type is items, create data objects inside that list data object.
              if (list && strcmp(child->Name(), "items") == 0)
              {
                  addListItems(domain, list, child);
              }
              else
              {
                  // Recurse.
                  addDataObjectsRecursively(domain, object, child);
              }
          }
      }
  4. In Kanzi Studio press F8 to exit the Preview.
  5. In Visual Studio in the Solution Explorer right-click the XML_data_source project and select Build to build the plugin.

Get list data from a data source

In this section you set a Grid List Box 2D node in the project to get data for its items from a list data object, which the plugin generates from the data source.

To get list data from a data source:

  1. In the Data Sources window next to the Cluster data source click and set the XML Data Source File property to the ClusterWithList.xml file. The ClusterWithList.xml file is included in the Visual Studio solution in the data source plugin project. You can find the file in the Application/bin directory.
    In addition to the data contained in the Cluster.xml file the ClusterWithList.xml file contains the data for list items.
  2. Click next to the Cluster data source to create the data objects from the contents of the ClusterWithList.xml file.
  3. In the Project select the Grid List Box node Contacts > ContactsList and in the Properties add and set the Item Template property to the ContactItem prefab.
    With the Item Template property you set which prefab template you want a Grid List Box node to use for the items in its list.
  4. From the Data Sources window drag the Cluster > contacts list data object to the Items Source property of the ContactsList node.
    With the Items Source property you set the data object which provides the data for the list items in a Grid List Box node.
  5. In the Prefabs in the ContactItem prefab select the ContactFirstName node, from the Data Sources window drag the Cluster > contacts > Item > contact > firstname data object, and drop it on the Text property in the Properties.
  6. Repeat the previous step for the ContactLastName and ContactNumber:


    The list of contacts now receives the data from the ClusterWithList.xml data source. Use a text editor to change the values in the ClusterWithList.xml file, save the ClusterWithList.xml file, and Kanzi Studio Preview and the Data Sources window show the updated data objects and their values.


< PREVIOUS STEP

What's next?

In this tutorial you learned how to define an XML data source and how to use the data from that data source in a Kanzi Studio project to provide the data to a Kanzi application. Now you can:

See also

To learn more about the data sources in Kanzi, see Data sources.